import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.XMLReader;
import java.io.*;

public class XMLParser {

	private ParserHandler handler;
	private SAXParser saxParser;
	
	public XMLParser(ParserHandler handler) {
	    this.handler = handler;
	    create();
	}

	private void create() {
		try {
			// Obtain a new instance of a SAXParserFactory.
			SAXParserFactory factory = SAXParserFactory.newInstance();
			// Specifies that the parser produced by this code will provide support for XML namespaces.
			factory.setNamespaceAware(true);
			// Specifies that the parser produced by this code will validate documents as they are parsed.
			//factory.setValidating(true);
			// Creates a new instance of a SAXParser using the currently configured factory parameters.
			saxParser = factory.newSAXParser();
	    	} catch (Throwable t) {
	    		t.printStackTrace();
	    	}
	  	}

	public void parse(String xml){
		try{
			InputStream is = new ByteArrayInputStream(xml.getBytes());
			handler.clear();
			saxParser.parse(is, handler);
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}
}